//
//  ViewController.swift
//  GPUImage
//
//  Created by 我演示 on 2019/11/14.
//  Copyright © 2019 Jerry-Fu. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    fileprivate lazy var filter = GPUImageBrightnessFilter()
    
    /// 图片存储地址
    var fileURL : URL {
        return URL(fileURLWithPath: "\(NSTemporaryDirectory())123.mp4")
    }
    
    fileprivate lazy var cammer : GPUImageVideoCamera? = GPUImageVideoCamera(sessionPreset: AVCaptureSession.Preset.high.rawValue, cameraPosition: .front   )
        let bilaterlFilter = GPUImageBilateralFilter() // 磨皮
          let exposureFilter = GPUImageExposureFilter() // 普光
          let brightnessFilter = GPUImageBrightnessFilter() // 美白
          let satureationFilter = GPUImageSaturationFilter() // 饱和
    
    fileprivate lazy var movieWriter : GPUImageMovieWriter = { [unowned self] in
        
        let writer = GPUImageMovieWriter(movieURL: self.fileURL , size: self.view.bounds.size)
        
        return writer!
    }()
          
    //2: C创建预览图层
    fileprivate lazy var preview : GPUImageView = GPUImageView(frame: self.view.bounds)
    
    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
      
        cammer?.outputImageOrientation = .portrait
        cammer?.horizontallyMirrorFrontFacingCamera = true
        
        //2: 创建预览图层
        view.insertSubview(preview, at: 0)
        
        //3: 获取滤镜组
        let filterGroup = getGroupFilters()
        
        //4: 设置GPUImage的响应组
        cammer?.addTarget(filterGroup)
        filterGroup.addTarget(movieWriter)
        
        //5: 开始采集视频
        cammer?.delegate = self
        //6: x设置writer的属性+
        
        
        
        
        
        
    }
    
    fileprivate func getGroupFilters() -> GPUImageFilterGroup {
        
        //1: 创建滤镜组
        let filterGroup = GPUImageFilterGroup()
        
        // 创建滤镜
        bilaterlFilter.addTarget(brightnessFilter)
        brightnessFilter.addTarget(exposureFilter)
        exposureFilter.addTarget(satureationFilter)
        
        //3: 设置滤镜组的初始化气垫粉
        filterGroup.initialFilters = [bilaterlFilter]
        filterGroup.terminalFilter = satureationFilter
        
        
        return filterGroup
        
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
     let sourceImage = UIImage(named: "1.jpeg")
        
        //2:使用GPUImage 高斯模糊
//        let picProcess = Pods_GPUImageVersionNumber
    let picProcess = GPUImagePicture(image: sourceImage)
        
        let blurFilter = GPUImageGaussianBlurFilter()
        //纹理烫
        blurFilter.texelSpacingMultiplier = 10
        
        picProcess?.addTarget(blurFilter)
        
        //2.3 处理图片
        blurFilter.useNextFrameForImageCapture()
        picProcess?.processImage()
        //2.4 取出最新的图片处理
       let newImage =  blurFilter.imageFromCurrentFramebuffer()
        
        //3: 显示最新的图片
        imageView.image = newImage
    }
    
    
    
}


extension ViewController {
    fileprivate func cammerFunc() {
        //1: 创建相机
        let camer = GPUImageVideoCamera(sessionPreset: AVCaptureSession.Preset.hd1920x1080.rawValue, cameraPosition: .back)
        camer?.delegate = self
        camer?.outputImageOrientation = .portraitUpsideDown
        
        //2: 添加滤镜
        camer?.addTarget(filter)
        let showView = GPUImageView(frame: view.bounds)
        //3:
        view.addSubview(showView)
        filter.addTarget(showView)
        
        //3: 开始采集画面
        camer?.startCapture()
        
    }
    
}


extension ViewController: GPUImageVideoCameraDelegate{
    func willOutputSampleBuffer(_ sampleBuffer: CMSampleBuffer!) {
        
    }
    
}
